//
//  ViewController.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/7.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class MainVC: LGTBaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //self.gotoMainVC()
        self.getAppInfo()
        
        self.setBlock()
    }
    
    func setBlock(){
        weak var weakSelf = self
        self.reloadBlock = { () -> Void in
            weakSelf?.getAppInfo()
        }
    }
    
    func getAppInfo(){
        
        var device_code:String = LGTOCBaseUtil.getIDFA()
        if device_code.count == 0{
            device_code = "0000-0000"
        }
        
        let parameters:[String:Any] = [
            "brand": "苹果",
            "model": LGTOCBaseUtil.getPhoneName(),
            "sys_version": SystemVersion,
            "device_code":device_code
        ]
        
        self.view.isUserInteractionEnabled = false
        weak var weakSelf = self
        weakSelf?.showLoading(loadingType: .NoNavAndTabbarLoadingType)
        LGTHttpUtil.httpUtil.requestData(url: APP_Init_URL, param: parameters, success: { (json) in
            //数据存储
            let appModel:AppModel = try! lgtDecode(AppModel.self, param: (json["data"] as! [String : Any]))
            AppModel.share = appModel
            weakSelf?.shareSDK(wxAppID: appModel.share_app_id, wxSecrect: appModel.share_app_secret)
            weakSelf?.gotoMainVC()
            weakSelf?.closeLoading()
            //调用获取appbadge 查看红点点
            LGTViewUtil.share.getAppBadge()
        }, failure:{ (json) in
            weakSelf?.showNoData(image: Net_NoData_Image, text: Net_Error_Text, subText: Net_Reload_Text, haveNav: false)
            weakSelf?.view.isUserInteractionEnabled = true
             weakSelf?.closeLoading()
        })
    }
    
    func shareSDK(wxAppID:String?, wxSecrect:String?){
        ShareSDK.registPlatforms { (platformsRegister: SSDKRegister?) in
            platformsRegister?.setupQQ(withAppId: "1109804997", appkey: "BYLkTm3uIBTSmQbK")
            platformsRegister?.setupWeChat(withAppId: wxAppID, appSecret: wxSecrect)
            
            
            //            ShareSDKConnector
            //    [ShareSDKConnector connectWeChat:[WXApi class]];
            //
            //    [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
        }
    }
    
    func gotoMainVC(){
        let appDelegate:AppDelegate = UIApplication.shared.delegate  as! AppDelegate
        //appDelegate.window = UIWindow.init(frame: CGRect.init(x: 0, y: 0, width: KScreenW, height: KScreenH))
        let mainTabVC:LGTTabBarVC = LGTTabBarVC()
        appDelegate.window!.rootViewController = mainTabVC
        
//        let appDelegate:AppDelegate = UIApplication.shared.delegate  as! AppDelegate
//        let testVC:LGTTestVC = LGTTestVC()
//        appDelegate.window!.rootViewController = testVC
    }
}

